package com.byit.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.builder.MapBuilder;
import com.byit.constant.DefaultValue;
import com.byit.domain.SysAppVersion;
import com.byit.enums.PromptInfo;
import com.byit.log.ChannelType;
import com.byit.log.Log;
import com.byit.log.OperatorType;
import com.byit.msg.MessageResult;
import com.byit.service.IAppNewsService;
import com.byit.service.ISysAppVersionService;
import com.byit.shrio.session.SessionHelper;
import com.byit.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysAppVersionController
 * @Description App版本管理
 * @Author lxp
 * @Date 2019-01-07 15:55
 **/
@RestController
@RequestMapping("/api/sys/sysAppVersion")
public class SysAppVersionController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysAppVersionService sysAppVersionService;


    @Autowired
    private IAppNewsService appNewsService;
    /**
     * 新增或修App版本管理信息
     *
     * @param session
     * @param sysAppVersion
     * @return
     */
    @PostMapping("/add")
    @Log(title = "App版本管理", operate = OperatorType.DATA_INSERT, channel = ChannelType.WEB_INTER)
    public Object insertAnnouncement( HttpSession session, SysAppVersion sysAppVersion) {


        Long userId = SessionHelper.getUserId(session);
        try {
            if (null == sysAppVersion) {
                return MessageResult.warpper(PromptInfo.ERROR_PARAM_NOT_NULL);
            }

                sysAppVersion.setCreateTime(new Date());
                sysAppVersion.setUpdateTime(new Date());
            //// 加密用户密码
            sysAppVersion.setOperationId(userId);

            sysAppVersionService.insertOrUpdate(sysAppVersion);
            return MessageResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return MessageResult.error(e);
        }
    }


    /**
     * 新增或修App版本管理信息
     *
     * @param session
     * @param sysAppVersion
     * @return
     */
    @PostMapping("/edit")
    @Log(title = "App版本管理", operate = OperatorType.DATA_UPDATE, channel = ChannelType.WEB_INTER)
    public Object editSysAppVersion( HttpSession session, SysAppVersion sysAppVersion) {


        Long userId = SessionHelper.getUserId(session);
        try {
            if (null == sysAppVersion) {
                return MessageResult.warpper(PromptInfo.ERROR_PARAM_NOT_NULL);
            }
            if (sysAppVersion.getId() != 0) {

                sysAppVersion.setUpdateTime(new Date());
            }
            //// 加密用户密码
            sysAppVersion.setOperationId(userId);

            sysAppVersionService.insertOrUpdate(sysAppVersion);
            return MessageResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return MessageResult.error(e);
        }
    }


    /**
     * 通过Id查询证件管理
     *
     * @param id
     * @return
     */
    @GetMapping("/details")
    @Log(title = "App版本管理", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
    public Object selectAppNewsById(@RequestParam("id") Integer id) {
        SysAppVersion sysAppVersion = sysAppVersionService.selectById(id);
        return MessageResult.success(sysAppVersion);
    }


    /**
     * 按照条件查询证件管理
     *
     * @param session
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    @Log(title = "App版本管理", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
    public Object selectAppNewsAll(HttpSession session,
                                   @RequestParam(value = "code", defaultValue = "") String code,
                                   @RequestParam(value = "version", defaultValue = "") String version,
                                   @RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
                                   @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit) {
        Page<SysAppVersion> pageInfo = new Page<>(page, limit);
        Map<String, Object> params = MapBuilder.newMap(4).setParam("code", code).ok();
        if (null != version && !"".equals(version)) {
            params.put("name", "%" + StringUtil.decodeSpecialCharsWhenLikeUseBackslash(version) + "%");
        }
        Page<JSONObject> result = sysAppVersionService.selectSysAppVersionAll(pageInfo, params);

        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, result);
    }

    /**
     * 逻辑删除证件管理信息
     *
     * @param session
     * @param ids
     * @return
     */
    @PostMapping("/del")
    @Log(title = "App版本管理", operate = OperatorType.DATA_DELETE, channel = ChannelType.WEB_INTER)
    public Object delAppNewsByIds(HttpSession session, @RequestParam("ids") String ids) {
        Long userId = SessionHelper.getUserId(session);
        List<Long> idList = StringUtil.splitLong(ids, ",");
        Map<String, Object> map = MapBuilder.newMap(3).setParam("updateTime", new Date()).setParam("operId", userId).ok();

        for (Long id : idList) {
            map.put("id",id);
            sysAppVersionService.delSysAppVersionById(map);
        }
        return MessageResult.success();
    }


    /**
     * 上传App安装包
     */
    @PostMapping("/upload")
    @Log(title = "app版本管理", operate = OperatorType.IMG_UPLOAD, channel = ChannelType.WEB_INTER)
    public Object upload(MultipartFile file) {

        String path = appNewsService.getPictrePath(file, "appVersion");
        try {
            if (null == path) {
                return MessageResult.error("文件不能为空");
            } else {
                return MessageResult.success(path);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return MessageResult.error(e);
        }


    }

}
